package com.chum.chamber.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.support.SessionStatus;

import com.chum.chamber.model.Registry;
import com.chum.chamber.service.RegistryService;
import com.chum.chamber.validator.RegistryValidator;

@Controller
@RequestMapping("/registry.htm")
public class RegistryController {

	RegistryValidator registryValidator;
	RegistryService registryService;
	
	@Autowired
	public RegistryController(RegistryValidator registryValidator, RegistryService registryService) {
		this.registryValidator = registryValidator;
		this.registryService = registryService;
	}
	
	@ExceptionHandler
	@RequestMapping(method = RequestMethod.POST)
	public String registrySubmit(@ModelAttribute("registry")Registry registry, BindingResult result, SessionStatus status) {
		String view = "RegistryForm";
		try {
			registryValidator.validate(registry, result);
			if (result.hasErrors()) {
				
			} else {
				registryService.saveRegistry(registry);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return view;
	}
	
	@RequestMapping(method = RequestMethod.GET)
	public String initForm(ModelMap model){
		Registry registry = new Registry();
		model.addAttribute("registry", registry);
		return "RegistryForm";
	}
}
